package com.atguigu.tingshu.order.startegy.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.anno.GuiGuLogin;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.google.common.collect.Lists;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.order.startegy.OrderTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AlbumOrderTradeStrategy implements OrderTradeStrategy {
    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {
        //获取当前用户id
        Long userId = AuthContextHolder.getUserId();
        UserInfo userInfo = userInfoFeignClient.getUserInfoById(userId).getData();
        //根据付款项目id查询专辑数据
        Long itemId = tradeVo.getItemId();
        AlbumInfo albumInfo = albumInfoFeignClient.getAlbumInfo(itemId).getData();
        //判断用户是否购买过该专辑
        Boolean data = userInfoFeignClient.isPaidAlbum(itemId, userId).getData();
        if(data){
            throw new GuiguException(ResultCodeEnum.REPEAT_BUY_ERROR);
        }


        //创建一个orderInfoVo对象 封装数据
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        orderInfoVo.setItemType(SystemConstant.ORDER_ITEM_TYPE_ALBUM);


        //设置相关金额
        BigDecimal originalAmount = albumInfo.getPrice();
        orderInfoVo.setOriginalAmount(originalAmount);
        Integer isVip = userInfo.getIsVip();
        long vipExpireTime = userInfo.getVipExpireTime().getTime();
        if("1".equals(isVip) && vipExpireTime > System.currentTimeMillis()){
            BigDecimal vipDiscount = albumInfo.getVipDiscount();
            if(vipDiscount.intValue() == -1){ //没有折扣
                orderInfoVo.setDerateAmount(new BigDecimal("0"));
                orderInfoVo.setOrderAmount(originalAmount);
            }else{ //有折扣
                //计算减免金额
                BigDecimal orderAmount = originalAmount.multiply(vipDiscount)
                        .multiply(new BigDecimal("10")).
                        divide(new BigDecimal("10"), 2, BigDecimal.ROUND_HALF_UP);
                orderInfoVo.setDerateAmount(originalAmount.subtract(orderAmount));
                orderInfoVo.setOrderAmount(orderAmount);
            }

        }else {
            BigDecimal discount = albumInfo.getDiscount();
            if(discount.intValue() == -1){ //没有折扣
                orderInfoVo.setDerateAmount(new BigDecimal("0.00"));
                orderInfoVo.setOrderAmount(originalAmount);
            }else{ //有折扣
                //计算减免金额
                BigDecimal orderAmount = originalAmount.multiply(discount)
                        .multiply(new BigDecimal("10")).
                        divide(new BigDecimal("10"), 2, BigDecimal.ROUND_HALF_UP);
                orderInfoVo.setDerateAmount(originalAmount.subtract(orderAmount));
                orderInfoVo.setOrderAmount(orderAmount);
            }
        }
        //封装订单明细
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(itemId);
        orderDetailVo.setItemName(albumInfo.getAlbumTitle());
        orderDetailVo.setItemUrl(albumInfo.getCoverUrl());
        orderDetailVo.setItemPrice(orderInfoVo.getOrderAmount());
        orderInfoVo.setOrderDetailVoList(Arrays.asList(orderDetailVo));

        //封装订单减免明细
        if(orderInfoVo.getDerateAmount().intValue() > 0){
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_ALBUM_DISCOUNT);
            orderDerateVo.setDerateAmount(orderInfoVo.getDerateAmount());
            orderDerateVo.setRemarks("专辑购买折扣");

            orderInfoVo.setOrderDerateVoList(Arrays.asList(orderDerateVo));
        }
        this.setOtherDataToOrderInfoVo(orderInfoVo,stringRedisTemplate);
        return orderInfoVo;
    }

    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_ALBUM;
    }
}
